﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Framework;
using TjZheng.Power.Business;
using TjZheng.Power.DataEntity;
using TjZheng.Power.RedisEntity;
using TjZheng.Quartz;
using TjZheng.Redis;

namespace TjZheng.SyncPowerRedis
{
    public class SyncUser : TjZhengJob
    {
        public override void JobExecute(IJobExecutionContext context)
        {
            bool flag = true;
            int pageIndex = 0;
            List<UserInfo> objUserInfoList = null;
            List<UserRole> objUserRoleList = null;
            BizUserInfo objBizUserInfo = new BizUserInfo();
            BizUserRoleInfo objBizUserRoleInfo = new BizUserRoleInfo();
            ConsoleHelper.WriteLineYellow(string.Format("[同步用户信息][{0}]：开始", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
            UserRole objUserRole = null;
            while (flag)
            {
                objUserInfoList = objBizUserInfo.GetPageList(pageIndex, 50);
                if (objUserInfoList.IsNull())
                {
                    flag = false;
                    ConsoleHelper.WriteLineYellow(string.Format("[同步用户信息][{0}]：结束", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                    break;
                }
                objBizUserInfo.StoreAll(objUserInfoList, PowerVariable.ProviderName, PowerVariable.Db);
                objUserRoleList = new List<UserRole>();
                foreach (var objUserInfo in objUserInfoList)
                {
                    objUserRole = new UserRole
                    {
                        UserId = objUserInfo.Id,
                        GroupIds = objBizUserRoleInfo.GetFieldList<long>("UserId=" + objUserInfo.Id, "GroupId")
                    };
                    objUserRoleList.Add(objUserRole);
                }
                RedisHelper.StoreAll<UserRole>(objUserRoleList, PowerVariable.ProviderName, PowerVariable.Db);
                pageIndex++;
                ConsoleHelper.WriteLineYellow(string.Format("[同步用户信息][{0}]：第{1}页数据同步完成", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), pageIndex));
            }
        }
    }
}
